import importlib

from re_common.baselibrary.utils.baseallstep import GlobalControl
from re_common.baselibrary.utils.basestring import BaseString
from re_common.baselibrary.utils.importfun import import_fun

dicts = {
    "1": {"mod": "step1_test", "fun": "main", "run": "step1_test.main()"},
    "2": {"mod": "step2_test", "fun": "main", "run": "step1_test.main()"},
    "3": {"mod": "step3_test", "fun": "main", "run": "step1_test.main()"},
}


class All(GlobalControl):

    def __init__(self):
        super().__init__(__file__)

    def set_step_dict(self, dicts):
        self.step_dicts = dicts
        return self

    def imports(self, dicts=None):

        if dicts:
            for k in dicts.keys():
                v = dicts[k]["mod"]
                modename = v
                if BaseString().find_sub(v, "."):
                    modename = v.split(".")[-1]
                model = importlib.import_module(v)
                import_fun(self, model, modename=modename)

    def step_fun(self):
        """
        调用方法的字符串
        :return:
        """
        if self.key not in self.step_dicts:
            return "end"
        return "self." + self.step_dicts[self.key]["run"]

    def one_check(self):
        """
        该步骤检查代码是否运行完成
        :return:
        """
        if self.key == "1":
            self.writetrue()
        elif self.key == "2":
            self.writetrue()
        elif self.key == "3":
            self.writetrue()
        else:
            return True


if __name__ == "__main__":
    a = All()
    a.set_step_dict(dicts)
    a.imports(dicts)
    a.work()
